﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Controls;

using BindingSource = System.Windows.Forms.BindingSource;

namespace Gamey.UI
{
    class GamesControlManager
    {
        IGamesControl gamesControl;
        Emulator emulator;
        GameyDataSet dataSet;
        BindingSource bindingSource;
        BackgroundWorker gameLoader;

        public GamesControlManager(IGamesControl gamesControl, Emulator emulator)
        {
            this.gamesControl = gamesControl;
            this.emulator = emulator;

            this.dataSet = new GameyDataSet();
            this.bindingSource = new BindingSource();
            this.gameLoader = new BackgroundWorker();
            this.gameLoader.DoWork += gameLoader_DoWork;
            this.gameLoader.RunWorkerCompleted += gameLoader_RunWorkerCompleted;

            this.gamesControl.DataGrid.MouseDoubleClick += dataGrid_MouseDoubleClick;
            this.gamesControl.SearchTextBox.TextChanged += searchTextBox_TextChanged;
        }

        public event EventHandler LoadedGames;

        protected void OnLoadedGames()
        {
            this.gamesControl.IndicateReady();

            EventHandler loadedGamesHandler = LoadedGames;
            if (loadedGamesHandler != null)
            {
                loadedGamesHandler(this, EventArgs.Empty);
            }
        }

        public void LoadGames()
        {
            if (!string.IsNullOrEmpty(this.emulator.RomsPath))
            {
                this.gamesControl.IndicateLoading();
                this.gameLoader.RunWorkerAsync();
            }
            else
            {
                OnLoadedGames();
            }
        }

        public void LaunchSelectedGame()
        {
            var selectedItem = this.gamesControl.DataGrid.SelectedItem as DataRowView;
            if (selectedItem == null)
            {
                return;
            }

            var selectedRow = selectedItem.Row as DataRow;
            if (selectedRow == null)
            {
                return;
            }

            this.emulator.LaunchGameFromDataRow(selectedRow);
        }

        void gameLoader_DoWork(object sender, DoWorkEventArgs e)
        {
            this.emulator.LoadGames(this.dataSet);
        }

        void gameLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.bindingSource.DataSource = this.dataSet;
            this.bindingSource.DataMember = this.emulator.GetTableFromDataSet(this.dataSet).TableName;
            this.gamesControl.DataGrid.ItemsSource = this.bindingSource;

            OnLoadedGames();
        }

        void dataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            LaunchSelectedGame();
        }

        private void searchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.bindingSource.Filter = string.Format("Name LIKE '{0}%' OR File LIKE '%{0}%'", this.gamesControl.SearchTextBox.Text);
        }
    }
}
